<?php

namespace app\controllers;

use app\models\Desk;
use app\models\Mall;
use app\models\Message;
use app\models\Slide;
use app\models\Upload;
use app\models\User;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\myclass;
use yii\base\Module;
use yii\db\Query;

class AdminController extends Controller
{
    public $identity;
    public function __construct($id, Module $module, array $config = [])
    {
        parent::__construct($id, $module, $config);
        $this->identity = Yii::$app->user->identity;
        if(is_null($this->identity))
        {
            header('Location:/login');
        }
    }
    #管理员专用操作
    private function adminMeth()
    {
        if($this->identity['type']!='admin')
        {
            header('Location:/login');
        }
    }

    /*店铺 列表页面*/
    public function actionMallList()
    {
        #管理员专用
        $this->adminMeth();

        $mall_md=new Mall();
        #分页按钮设置
        $base_url='/index.php/admin/mall-list/?';
        $data_total=$mall_md->getCount(array('is_del'=>'0'));
        $page_size=10;
        $page_index=isset($_GET['page'])?$_GET['page']:1;
        $page=new myclass\Page($data_total,$base_url,$page_index,$page_size);
        $page_block=$page->getPageBlock();
        $offset=$page->getLimit()[0];
        $limit=$page->getLimit()[1];
        #获取列表
        $list=$mall_md->getAll($offset,$limit);
        $params=array(
            'list'=>$list,
            'page_block'=>$page_block
        );
        return $this->render('mall_list',$params);

    }

    /*店铺 编辑页面*/
    public function actionMallEdit($id)
    {
        #管理员专用
        $this->adminMeth();

        $mall_md=new Mall();
        $info=$mall_md->getOneById($id);
        $user_md=new User();
        $info['user_info']=$user_md->getOneByMallId($id);
        $params=array(
            'info'=>$info,
        );
        return $this->render('mall_edit',$params);

    }

    /*店铺 新建页面*/
    public function actionMallNew()
    {
        #管理员专用
        $this->adminMeth();
        return $this->render('mall_new');

    }

    /*桌面 列表页面*/
    public function actionDeskList()
    {
        switch ($this->identity['type'])
        {
            case 'admin':
                $params=$this->adminDeskList();
                return $this->render('desk_list',$params);
                break;
            case 'normal':
                $params=$this->normalDeskList();
                return $this->render('desk_list',$params);
                break;
            default:
                header('Location:/login');
                break;
        }
    }
    /*管理员 桌子列表 admin*/
    private function adminDeskList()
    {
        $desk_md=new Desk();
        #分页按钮设置
        $base_url='/index.php/admin/desk-list/?';
        $data_total=$desk_md->getCount(array('is_del'=>'0'));
        $page_size=10;
        $page_index=isset($_GET['page'])?$_GET['page']:1;
        $page=new myclass\Page($data_total,$base_url,$page_index,$page_size);
        $page_block=$page->getPageBlock();
        $offset=$page->getLimit()[0];
        $limit=$page->getLimit()[1];
        #获取列表
        $list=$desk_md->getAll($offset,$limit);
        $params=array(
            'list'=>$list,
            'page_block'=>$page_block
        );
        return $params;
    }

    /*普通用户 桌子列表 normal*/
    private function normalDeskList()
    {
        $desk_md=new Desk();
        $mall_id=$this->identity['mall_id'];
        #分页按钮设置
        $base_url='/index.php/admin/desk-list/?';
        $data_total=$desk_md->getCount(array('is_del'=>'0','mall_id'=>$mall_id));
        $page_size=10;
        $page_index=isset($_GET['page'])?$_GET['page']:1;
        $page=new myclass\Page($data_total,$base_url,$page_index,$page_size);
        $page_block=$page->getPageBlock();
        $offset=$page->getLimit()[0];
        $limit=$page->getLimit()[1];
        #获取列表
        $list=$desk_md->getListByMallId($mall_id,$offset,$limit);
        $params=array(
            'list'=>$list,
            'page_block'=>$page_block
        );
        return $params;
    }

    /*桌面 编辑页面*/
    public function actionDeskEdit($id)
    {
        $desk_md=new Desk();
        $info=$desk_md->getOneById($id);
        $params=array(
            'info'=>$info,
        );
        return $this->render('desk_edit',$params);
    }

    /*桌面 新建页面*/
    public function actionDeskNew()
    {
        $mall_md=new Mall();
        $type=$this->identity['type'];
        switch ($type)
        {
            case 'admin':
                $mall_list=$mall_md->getAll(0,50);
                $params=array(
                    'mall_list'=>$mall_list,
                );
                break;
            case 'normal':
                $mall_info=$mall_md->getOneById($this->identity['mall_id']);
                $mall_list=array(
                    array(
                        'id'=>$mall_info['id'],
                        'name'=>$mall_info['name'],
                    ),
                );
                $params=array(
                    'mall_list'=>$mall_list,
                );
                break;
            default:
                header('Location:/login');
                break;
        }

        return $this->render('desk_new',$params);
    }

    /*信息 列表页面*/
    public function actionMsgList($id)
    {
        $msg_md=new Message();
        #分页按钮设置
        $base_url='/index.php/admin/msg-list/?id='.$id.'&';
        $data_total=$msg_md->getCount(array('is_del'=>'0','desk_id'=>$id));
        $page_size=10;
        $page_index=isset($_GET['page'])?$_GET['page']:1;
        $page=new myclass\Page($data_total,$base_url,$page_index,$page_size);
        $page_block=$page->getPageBlock();
        $offset=$page->getLimit()[0];
        $limit=$page->getLimit()[1];
        #获取桌子信息
        $desk_md=new Desk();
        $desk_info=$desk_md->getOneById($id);
        #获取列表
        $list=$msg_md->getListByDeskId($id,$offset,$limit);
        $params=array(
            'list'=>$list,
            'desk_info'=>$desk_info,
            'page_block'=>$page_block,
            'list_page'=>$page_index,
        );
        return $this->render('msg_list',$params);
    }

    /*信息 编辑页面*/
    public function actionMsgEdit($id,$list_page)
    {
        $msg_md=new Message();
        $info=$msg_md->getOneById($id);
        $params=array(
            'info'=>$info,
            'list_page'=>$list_page,

        );
        return $this->render('msg_edit',$params);
    }

    /*信息 新建页面*/
    public function actionMsgNew()
    {
        return $this->render('msg_new');
    }

    /*轮播 编辑界面*/
    public function actionSlideEdit()
    {
        #管理员专用
        $this->adminMeth();

        $slide_md=new Slide();
        $out_list=array();
        $list=$slide_md->getlistAll();
        foreach ($list as $temp)
        {
            $out_list[$temp['name']]=$temp['url'];
        }
        $param=array(
            'list'=>$out_list,
        );
        return $this->render('slide_edit',$param);
    }

    /*密码修改 界面*/
    public function actionPassChange()
    {

        return $this->render('pass_change');

    }
    ################test######################
    public function actionTest()
    {
        $slide_md=new Slide();
//        $re=$slide_md->createNew('name_test','url_test');
//        $re=$slide_md->getoneByName('name_test2');
//        $re=$slide_md->autoUpdate('name_test2','url');
        $re=$slide_md->getlistAll();
        $test='';
    }
    ################test######################

}
